import Toast from '@vant/weapp/toast/toast'
const request = ({url,data,success,header,loading}) => {
 
  if(loading !==false){
    wx.showLoading({  
      title: '加载中',  
      mask: true // 显示透明蒙层，防止触摸穿透  
    });  
  }
  wx.getStorage({
    key:'token',
    success(res){
    },
    fail(){
    },
    complete(cres){
      wx.request({
        url:getApp().globalData.base + url,
        method:'POST',
        header: header||{
          'content-type': 'application/x-www-form-urlencoded',
          'accessToken': cres.data
        },
        data:data,
        success (res) {
          success && success(res)
        },
        complete(res){
          wx.hideLoading(); // 隐藏加载提示  
          // if(loading !==false){
          //   wx.hideLoading(); // 隐藏加载提示  
          // }
          if(res.data.msg == '认证失败，请重新认证！'){
            Toast(res.data.msg)
            wx.removeStorage({key:'token'})
            wx.removeStorage({key:'userInfo'})
            wx.removeStorage({key:'schoolInfo'})
            setTimeout(()=>{
              wx.navigateTo({
                url: '../login/login'
              })
            },1000)
          }
        }
      })
    }
  })


}
module.exports = {
  request
}
